package com.lmx.repoDomain.repository;

import com.lmx.common.constants.CommonConstants;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.CustomerService;
import com.lmx.persistence.service.EmployeeService;
import com.lmx.repoDomain.tmodel.NursingLog;
import com.lmx.repoDomain.tmodel.NursingProject;
import com.lmx.repoDomain.tmodel.WorkDelivery;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

@JsonApiResourceRepository(WorkDelivery.class)
@Component
public class WorkDeliveryRepository extends BaseRepository<WorkDelivery> {

	@Autowired
	private EmployeeService employeeService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(WorkDelivery.class);
	}

	
	@Override
	@JsonApiFindAll
	public Iterable<WorkDelivery> findAll(final QueryParams requestParams) {
		employeeService.deleteNullWorkDelivery();
		List data = new ArrayList();
		data = (List) super.findAll(requestParams);
		return data;
	}
	
	
	@Override
	@JsonApiSave
	public WorkDelivery save(WorkDelivery data) {		
        if(data.getId() == null){
        	//设置已入住人数、请假/退住老人、新入住老人
        	employeeService.createWorkDelivery(data);
        }else {
        	if(data.getDelStatus()==1){
                dslJpaService.update(data);
        	}else{
            	WorkDelivery oldData=employeeService.getWorkDelivery(data);
            	//新老数据都为已完成不允许修改
            	if(CommonConstants.DICTTYPE.WORKDELIVERY_STATUS2.label().equals(oldData.getStatus().getTypecode())&&CommonConstants.DICTTYPE.WORKDELIVERY_STATUS2.label().equals(data.getStatus().getTypecode())){
            		return oldData;
            	}
            	//数据已修改为完成也不修改
            	if(CommonConstants.DICTTYPE.WORKDELIVERY_STATUS2.label().equals(oldData.getStatus().getTypecode())&&CommonConstants.DICTTYPE.WORKDELIVERY_STATUS1.label().equals(data.getStatus().getTypecode())){
            		return oldData;
            	}
                dslJpaService.update(data);
        	}
        }		
		return data;
	}
	
	
}
